const express = require('express'),
    router = express.Router()
const async = require('async')
const request = require('request')
const moment = require('moment');


// const appID = "wxa7ca115bf6820c7f"; //test
// const appsecret = "17dc9e2c03220d7e4903a1580a09a53d";


const appID = "wxadf37cdbc669ec6e";
const appsecret = "2d9da312ca3028e5be28d97ba3ebf28a";


module.exports.getAccessToken = function getAccessToken(callback) {
    redisClient.get('access_token', function(err, data) {
            if (data == null || data == 'undefined') {
                // callback(getAccessTokenFromWeb(function(e) { return e }))
                var wxurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appsecret
                request({
                    url: wxurl,
                    method: "GET",
                    timeout: 10000,
                    followRedirect: true,
                    maxRedirects: 10
                }, function(error, response, body) {
                    redisClient.set('access_token', JSON.parse(body).access_token, function(err, success) {
                        redisClient.expire('access_token', parseInt(JSON.parse(body).expires_in), function(err, ress) {
                            callback(JSON.parse(body).access_token)
                        })
                    })
                })
            } else {
                // console.log('had', data)
                callback(data)
            }

        })
        // getToken(function(access_token) {
        //     return access_token
        // })
}


module.exports.refreshToken = function refreshToken(callback) {
    getAccessTokenFromWeb(function(access_token) {
        return access_token
    })
}


module.exports.cardTicket = function getAccessToken(callback) {
    redisClient.get('card_ticket', function(err, data) {
            if (data == null || data == 'undefined') {
                // callback(getTicketFromWeb(function(e) {}))
            redisClient.get('access_token', function(err, token) {
                if (token == null || token == 'undefined') {
                    var wxurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appsecret
                    request({
                        url: wxurl,
                        method: "GET",
                        timeout: 10000,
                        followRedirect: true,
                        maxRedirects: 10
                    }, function(error, response, body) {
                        redisClient.set('access_token', JSON.parse(body).access_token, function(err, success) {
                            redisClient.expire('access_token', parseInt(JSON.parse(body).expires_in), function(err, ress) {
                                request({
                                    url: "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + JSON.parse(body).access_token + "&type=wx_card",
                                    method: "GET",
                                    timeout: 10000,
                                    followRedirect: true,
                                    maxRedirects: 10
                                }, function(error, response, body) {
                                    // console.log('local', body)
                                    redisClient.set('card_ticket', JSON.parse(body).ticket, function(err, success) {
                                        redisClient.expire('card_ticket', parseInt(JSON.parse(body).expires_in), function(err, ress) {
                                            callback(JSON.parse(body).ticket)
                                        })
                                    })
                                })
                            })
                        })
                    })
                } else {
                    request({
                        url: "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=wx_card",
                        method: "GET",
                        timeout: 10000,
                        followRedirect: true,
                        maxRedirects: 10
                    }, function(error, response, body) {
                        // console.log('local', body)
                        redisClient.set('card_ticket', JSON.parse(body).ticket, function(err, success) {
                            redisClient.expire('card_ticket', parseInt(JSON.parse(body).expires_in), function(err, ress) {
                                callback(JSON.parse(body).ticket)
                            })
                        })
                    })
                }
            })              

            } else {
                // console.log('had', data)
                callback(data)
            }

        })
        // getToken(function(access_token) {
        //     return access_token
        // })
}




function getAccessTokenFromWeb(callback) {
    // console.log(appID, appsecret)
    var wxurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appsecret
    request({
        url: wxurl,
        method: "GET",
        timeout: 10000,
        followRedirect: true,
        maxRedirects: 10
    }, function(error, response, body) {
        redisClient.set('access_token', JSON.parse(body).access_token, function(err, success) {
            redisClient.expire('access_token', parseInt(JSON.parse(body).expires_in), function(err, ress) {
                callback(JSON.parse(body).access_token)
            })
        })
    })
}


function getTicketFromWeb(callback) {
    // var wxurl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=wx_card" + appID + "&secret=" + appsecret
    redisClient.get('access_token', function(err, data) {
            if (data == null || data == 'undefined') {

                getAccessTokenFromWeb(function(e) {
                    request({
                        url: "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + e + "&type=wx_card",
                        method: "GET",
                        timeout: 10000,
                        followRedirect: true,
                        maxRedirects: 10
                    }, function(error, response, body) {
                        // console.log('web', body)
                        redisClient.set('card_ticket', JSON.parse(body).ticket, function(err, success) {
                            redisClient.expire('card_ticket', parseInt(JSON.parse(body).expires_in), function(err, ress) {
                                callback(JSON.parse(body).ticket)
                            })
                        })
                    })
                })
            } else {
                request({
                    url: "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + data + "&type=wx_card",
                    method: "GET",
                    timeout: 10000,
                    followRedirect: true,
                    maxRedirects: 10
                }, function(error, response, body) {
                    // console.log('local', body)
                    redisClient.set('card_ticket', JSON.parse(body).ticket, function(err, success) {
                        redisClient.expire('card_ticket', parseInt(JSON.parse(body).expires_in), function(err, ress) {
                            callback(JSON.parse(body).ticket)
                        })
                    })
                })
            }
        })
        // getToken(function(access_token) {
        //     return access_token
        // })







    // request({
    //     url: wxurl,
    //     method: "GET",
    //     timeout: 10000,
    //     followRedirect: true,
    //     maxRedirects: 10
    // }, function(error, response, body) {

    //     redisClient.set('access_token', JSON.parse(body).access_token, function(err, success) {
    //         redisClient.expire('access_token', parseInt(JSON.parse(body).expires_in), function(err, ress) {
    //             callback(JSON.parse(body).access_token)
    //         })
    //     })
    // })
}


function getToken(callback) {
    redisClient.get('access_token', function(err, data) {
        if (data == null || data == 'undefined') {

            callback(getAccessTokenFromWeb(function(e) {}))
        } else {
            // console.log('had', data)
            callback(data)
        }

    })
}